swfobject.registerObject("dmtvPlayer", "9.0.0", "expressInstall.swf", playerSetup);
var dmtv, commentShow = true;
function notimplemented(){
	$("#clipTitle").css('background-color','#ff0000');
	$("#clipTitle").html('AVISO\n\nFunção não implementada nesta versão. Entre em contato com:\n\tarthur@dm.com.br');
}
function as_detail(){notimplemented();}
function as_comments(){
	l = $('#lightMask');
	if (l.css('opacity') != 0 && commentShow) { as_lights(); }
	
	var cmt = $("#comentarios");
	var ccmt = $("#comentariosContainer");
	if (commentShow) {
		var vid = 2932;
		ccmt.css('display','block').slideUp(0,function (){ccmt.slideDown('fast',
		function(){
			cmt.scrollTop(cmt.attr("scrollHeight")-300);
		}
		);});
		/*cmt.html("<center><img src='images/loader.gif' style='margin-top:100px'/></center>");
		$.post("tools/comentarios.php",{vid:id},function (comments){
			cmt.html(comments);
		});*/
		commentShow = false;
	} else {
		/*cmt.scrollTop(600);*/
		ccmt.css('display','block').slideUp('fast');
		commentShow = true;
	}
}
function as_help(){/*showbaloon*/}
function as_share(){notimplemented();}
function as_popout()
{
	var player_width = 640, player_height = 360;
	var top = (screen.availHeight*0.5)-(player_height*0.5),
		left = (screen.availWidth*0.5)-(player_width*0.5);
	var conteudo = "<html><head><title>DMTV - A SUA TV NA WEB</title><link rel='stylesheet' type='text/css' media='all' href='embed.css' /></head><body bgcolor='#000000'><center><font family=''color='#FFFFFF'>AQUI VEM O PLAYER DESTACADO</font></center></body></html>";
	var properties = 'width='+player_width+', height='+player_height+', top='+top+', left='+left+', menubars=0, toolbars=0, location=0, status=0, resizable=0, scrollbars=0, directories=0, personalbar=0, minimizable=0, titlebar=0';
	var embed = window.open('','dmtv',properties);
	embed.document.write(conteudo);
}
function as_lights(){
	l = $('#lightMask');
	if(l.css('opacity')==0){l.css({'display':'block'}).animate({'opacity':0.85,'-moz-opacity':0.85},600);}
	else{l.animate({'opacity':0,'-moz-opacity':0},600,function (){l.css({'display':'none'});});}
}
function toggleMask(value, opacity, zindex)
{
	l = $('#lightMask');
	if (value == 1) l.css('z-index',zindex).css('opacity',opacity).css('display','block');
	else l.css('z-index','1').css('opacity','.0').css('display','none');
}
$(document).ready(function () {
	$.ajax({
		type: "GET",
		url: "programas.xml",
		dataType: "xml",
		success: function(xml)
		{
			$(xml).find('video').each(function(){
				var id = $(this).attr('id'),
					title = $(this).attr('title'),
					source = $(this).find('source').text(),
					desc = $(this).find('desc').text();
				$('#programas').append('<label class="playVideo" id="link_'+id+'" video-url="'+source+'" title="'+title+'">'+title+'</label>');
			});
			$(".playVideo").click(function () {
				var url = $(this).attr("video-url");
				var title = $(this).attr("title");
				sendData(url);
				$('#clipTitle').fadeOut(300,function(){
					$(this).html("<span class='titlebar'>"+title+"</span>").fadeIn(500);
				});
			});
		}
	});
	$.get("login/loginchecker.php",function(result){
		if (result == "NoSession")
			drawLoginBar();
		else
			drawLoggedBar(result);
	});
	function drawLoggedBar(nome)
	{
		if(!$("#login").is(':hidden')) $("#login").toggle();
		if ($("#loginInfo").is(':hidden')) $("#loginInfo").toggle();
		$(".loginfo").html("Olá, "+nome+" | <strong id='signoff' style='cursor:pointer;'>Sair</strong>");
		$("#signoff").click(function(){
			$("#loginStatus").html("");
			$.get("login/logout.php",function(){drawLoginBar();});
		});
	}
	function drawLoginBar()
	{
		$(".loginfo").html("<strong id='logar' style='cursor:pointer;'>Logar</strong> | Esqueceu a senha? | <label id='registrar' style='cursor:pointer;'>Registrar</label>");
		$("#logar").click(function(){
			toggleMask(1, 0, 3);
			$("#lightMask").click(function(){closelogin();});
			var A=$("#login");
			A.css('width','200px').css('height','125px');
			var B=$(this);
			var C=B.offset();
			$("#login").css({top:C.top+B.height(),left:C.left-A.width()+A.width()});
			$("#login").css('display','block');
			$("#txtLogin").focus();
		});
		
		$("#login").keydown(function(e){
			switch(e.keyCode)
			{
				case 27:
					closelogin();
				break;
				case 13:
					submitLoginInfo();
				break;
			}
		});
		$("#botaoLogar").click(submitLoginInfo);
		$("#fecharLogin").click(function(){closelogin();});
		
		function closelogin()
		{
			$("#login").css('display','none');
			$(window).focus();
			toggleMask(0, 0, 1);
		}
		function closeform()
		{
			toggleMask(0);
			$("#formRegistrar").replaceWith("");
		}
		$("#registrar").click(function(){
			toggleMask(1, .4, 3);
			$("#lightMask").click(function(){closeform();});
			$('body').append("<div id='formRegistrar' class='popupWindow' style='width:400px; height:398px; display:block;'></div>");
			var form = $('#formRegistrar');
			form.css('left',$(document).width()*.5-form.width()*.5).css('top',$(window).height()*.5-form.height()*.5).css('text-align','center').html("<img style='margin-top:"+form.height()*.5+"px;' src='images/loader.gif'/>");
			$.get("login/registrar.php",function(data){
				form.css('text-align','left');
				form.html(data);
				$("#txtUsuario").focus();
				$('#fecharRegistro').click(function(){closeform();});
				form.keydown(function(e){
					switch(e.keyCode)
					{
						case 27:
							closeform();
						break;
						case 13:
							submitRegInfo();
						break;
					}
				});
			});
		});
	}
	
	function submitLoginInfo()
	{
		var usr = $("#txtLogin").val();
		var pwd = $("#txtSenha").val();
		$("#loginInfo").toggle();
		$("#loginStatus").css('text-align','center').css('padding-top','50px').html("<img src='images/loader.gif'/>");
		$.post("login/autenticacao.php",{usuario:usr,senha:pwd},function(xml)
		{
			var nome = $(xml).find('user').text();
			if (nome != "")
			{
				drawLoggedBar(nome);
				toggleMask(0, 0, 1);
			}
			else {
				$("#loginStatus").html("<font color='#d02626'>Dados inválidos.</font><br><strong id='voltar' style='cursor:pointer;'>Voltar</strong>");
				$('#voltar').click(function(){
					$("#loginInfo").toggle();
					$("#txtLogin").focus();
				});
			}
		});
	}
});

$(window).resize(function () {
	$("#lightMask").css('height', '100%');
});

function playerSetup(e) {
	if (e.success) dmtv = e.ref;
}
function sendData(videoURL) {
	dmtv.playVideo(videoURL);
}

function setFocus() {
	dmtv.focus();
}